<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jinzhao
 * Date: 16-7-26
 * Time: 下午4:48
 */

class Cate extends MY_Controller {
    public function index()
    {
        $this->load->model('admin/cate_model');
        $result = $this->cate_model->get();
        $data = array(
            'cate_table' => $result
        );
        $this->load->view('admin/cate.html', $data);
    }
    public function add_cate()
    {
        $this->load->helper('form');
        $this->load->view('admin/add_cate.html');
    }
    public  function  add_cate_doing()
    {
        $this->load->library('form_validation');
        $this->load->helper('form');
        $ret = $this->form_validation->run('cate');
        if (!$ret){
            $this->load->view('admin/add_cate.html');
        }else {
            $this->load->model('admin/cate_model');
            
            $data = array(
                'cname' => $this->input->post('cname')
            );
            $this->cate_model->add($data);
            header('Location:'.base_url('admin/cate.html'));
        }
    }
    
    public  function  update_cate_ajax()
    {
        $this->load->library('form_validation');
        $this->load->helper('form');
        $ret = $this->form_validation->run('cate_name');
        
        if (!$ret){
            echo form_error('cid');
            echo form_error('cname');
            return ;
        }
        $data = array(
            'cid' => array(
                'cid' => $this->input->post('cid')
            ),
            'cname' => array(
                'cname' => $this->input->post('cname')
            )    
        );
        $this->load->model('admin/cate_model');
        $ret = $this->cate_model->update($data);
        if (!$ret){
            echo '修改失败';
        } else {
            echo 'ok';
        }
    }
    
    public function del_cate()
    {
        $this->load->library('form_validation');
        $this->load->helper('form');
        $ret = $this->form_validation->run('del_cate');
        if (!$ret){
            echo form_error('cid');
            echo form_error('cname');
            return;
        }
        $data = array(
            'cid' => $this->input->post('cid') 
        );
        $this->load->model('admin/cate_model');
        $ret = $this->cate_model->del($data);
        if (!$ret){
            echo '删除失败';
            return;
        }
        echo 'ok'; 
    }
}









